﻿define([
    "dojo/_base/declare"
    , "esri/layers/GraphicsLayer"
], function (declare
    , GraphicsLayer) {
    var ExGraphicsLayer = declare([GraphicsLayer], {
            setData: function (mapView, layer) {
                var me = this;
                var mapUtils = mapView.mapUtils;
                var options = layer.layer || {};
                var data = $.extend(true, [], options.data);
                var _geometryType = options.geometryType;
                if (!data) {
                    if (typeof options.callback == 'function') {
                        options.callback(layer);
                    }
                    ;
                    return
                }
                ;
                if (_geometryType) {
                    switch (_geometryType) {
                        case mapUtils.Enum.GType.Point:
                        case mapUtils.Enum.GType.Multipoint:
                        case "esriGeometryPoint":
                            me.setPointData(mapView, layer);
                            break;
                        case mapUtils.Enum.GType.Polyline:
                        case "esriGeometryPolyline":
                            me.setPolylineData(mapView, layer);
                            break;
                        case  mapUtils.Enum.GType.Extent:
                        case  mapUtils.Enum.GType.Circle:
                        case mapUtils.Enum.GType.Polygon:
                        case "esriGeometryPolygon":
                            me.setPolygonData(mapView, layer);
                            break;
                        case mapUtils.Enum.GType.Text:
                            me.setTextData(mapView, layer);
                            break;
                    }
                    ;
                } else {
                    data.map(function (item) {
                        _geometryType = item.geometryType || mapUtils.Enum.GType.Point;
                        var options = {
                            layer: $.extend({}, layer.layer)
                        };
                        options.layer.data = [item];
                        switch (_geometryType) {
                            case mapUtils.Enum.GType.Point:
                                me.setPointData(mapView, options);
                                break;
                            case mapUtils.Enum.GType.Polyline:
                                me.setPolylineData(mapView, options);
                                break;
                            case mapUtils.Enum.GType.Extent:
                            case mapUtils.Enum.GType.Circle:
                            case mapUtils.Enum.GType.Polygon:
                                me.setPolygonData(mapView, options);
                                break;
                            case mapUtils.Enum.GType.Text:
                                me.setTextData(mapView, options);
                                break;
                        }
                        ;
                    });
                }
                ;
            },
            setPointData: function (mapView, layer) {
                var me = this;
                var data = layer.layer.data || [];
                var options = layer.layer || {};
                var lngLatArray = mapView.mapUtils.toLngLatArray(mapView, data, options);
                var symbolFieldName = options.symbolFieldName || mapView.getSymbolFieldName();

                lngLatArray.map(function (lngLat, index) {
                    var point = mapView.mapUtils.createPoint(mapView, lngLat);
                    var symbol = lngLat[symbolFieldName] || options.symbol || 'common_point';

                    mapView.mapUtils.getSymbol(mapView, symbol, function (symbol) {
                        var attributes = lngLat;
                        var graphic = mapView.mapUtils.createGraphic(mapView, {
                            geometry: point,
                            symbol: symbol,
                            attributes: attributes,

                        });
                        me.add(graphic);
                        if (index == lngLatArray.length - 1) {
                            if (typeof options.callback == 'function') {
                                options.callback(layer);
                            }
                            ;
                        }
                        ;
                    });
                });
            },
            setPolylineData: function (mapView, layer) {
                var me = this;
                var data = layer.layer.data || [];
                var options = layer.layer || {};

                var symbolFieldName = options.symbolFieldName || mapView.getSymbolFieldName();
                var pathsFieldName = options.pathsFieldName || 'paths';
                data.map(function (item, index) {
                    if (pathsFieldName) {
                        item.paths = item[pathsFieldName];
                    }
                    ;

                    var polyline = mapView.mapUtils.createPolyline(mapView, item);

                    var symbol = item[symbolFieldName] || options.symbol || 'common_polyline';
                    mapView.mapUtils.getSymbol(mapView, symbol, function (symbol) {
                        var attributes = item;
                        var graphic = mapView.mapUtils.createGraphic(mapView, {
                            geometry: polyline,
                            symbol: symbol,
                            attributes: attributes
                        });
                        me.add(graphic);
                        if (index == data.length - 1) {
                            if (typeof options.callback == 'function') {
                                options.callback(layer);
                            }
                            ;
                        }
                        ;
                    });
                })
            },
            setPolygonData: function (mapView, layer) {
                var me = this;
                var mapUtils = mapView.mapUtils;
                var data = layer.layer.data || [];
                var options = layer.layer || {};
                var symbolFieldName = options.symbolFieldName || mapView.getSymbolFieldName();
                var ringsFieldName = options.ringsFieldName || 'rings';
                data.map(function (item, index) {
                    if (ringsFieldName) {
                        item.rings = item[ringsFieldName]
                    }
                    ;

                    var polygon = mapUtils.createPolygon(mapView, item);
                    var symbol = item[symbolFieldName] || options.symbol || 'common_polygon';
                    mapUtils.getSymbol(mapView, symbol, function (symbol) {
                        var attributes = item;
                        var graphic = mapUtils.createGraphic(mapView, {
                            geometry: polygon,
                            symbol: symbol,
                            attributes: attributes
                        });
                        me.add(graphic);
                        if (index == data.length - 1) {
                            if (typeof options.callback == 'function') {
                                options.callback(layer);
                            }
                            ;
                        }
                        ;
                    });
                })
            },
            setTextData: function (mapView, layer) {
                var me = this;
                var mapUtils = mapView.mapUtils;
                var data = layer.layer.data || [];
                var options = layer.layer || {};
                var lngLatArray = mapUtils.toLngLatArray(mapView, data, options);
                var symbolFieldName = options.symbolFieldName || mapView.getSymbolFieldName();
                var textFieldName = options.textFieldName;
                lngLatArray.map(function (lngLat, index) {
                    var point = mapUtils.createPoint(mapView, lngLat);
                    var symbol = lngLat[symbolFieldName] || options.symbol || 'common_text';

                    mapUtils.getSymbol(mapView, symbol, function (symbol) {
                        if (typeof symbol == 'object') {
                            symbol = mapUtils.$.extend({}, symbol);
                            if (lngLat[textFieldName] || lngLat[textFieldName] == 0) {
                                symbol.setText(lngLat[textFieldName]);
                                var attributes = lngLat;
                                var graphic = mapUtils.createGraphic(mapView, {
                                    geometry: point,
                                    symbol: symbol,
                                    attributes: attributes
                                });
                                me.add(graphic);
                                if (index == lngLatArray.length - 1) {
                                    if (typeof options.callback == 'function') {
                                        options.callback(layer);
                                    }
                                    ;
                                }
                                ;
                            }
                            ;
                        }
                        ;
                    });
                });
            },
            getGraphicLocation: function (mapView, esriEvent) {
                var me = this;
                var location = null;
                var mapUtils = mapView.mapUtils;
                var _geometryType = esriEvent.graphic.geometry.type;
                switch (_geometryType) {
                    case mapUtils.Enum.GType.Point:
                        location = me.getPointGraphicLocation(mapView, esriEvent);
                        break;
                    case mapUtils.Enum.GType.Polyline:
                        location = me.getPolylineGraphicLocation(mapView, esriEvent);
                        break;
                    case  mapUtils.Enum.GType.Extent:
                    case  mapUtils.Enum.GType.Circle:
                    case mapUtils.Enum.GType.Polygon:
                        location = me.getPolygonGraphicLocation(mapView, esriEvent);
                        break;
                    case mapUtils.Enum.GType.Text:
                        location = me.getTextGraphicLocation(mapView, esriEvent);
                        break;
                }
                ;
                return location;
            },
            getPointGraphicLocation: function (mapView, esriEvent) {
                if (!esriEvent || !esriEvent.graphic) {
                    return;
                }
                var geometry = esriEvent.graphic.geometry || {};

                var lngFieldName = mapView.getLngFieldName();
                var latFieldName = mapView.getLatFieldName();
                var location = {};
                location[lngFieldName] = geometry.x;
                location[latFieldName] = geometry.y;
                return location;
            },
            getPolylineGraphicLocation: function (mapView, esriEvent) {
                if (!esriEvent || !esriEvent.mapPoint) {
                    return;
                }
                var geometry = esriEvent.mapPoint || {};

                var lngFieldName = mapView.getLngFieldName();
                var latFieldName = mapView.getLatFieldName();
                var location = {};
                location[lngFieldName] = geometry.x;
                location[latFieldName] = geometry.y;
                return location;
            },
            getPolygonGraphicLocation: function (mapView, esriEvent) {
                if (!esriEvent || !esriEvent.mapPoint) {
                    return;
                }
                var geometry = esriEvent.mapPoint || {};
                var lngFieldName = mapView.getLngFieldName();
                var latFieldName = mapView.getLatFieldName();
                var location = {}
                location[lngFieldName] = geometry.x;
                location[latFieldName] = geometry.y;
                return location;
            },
            getTextGraphicLocation: function (mapView, esriEvent) {
                if (!esriEvent || !esriEvent.graphic) {
                    return;
                }
                var geometry = esriEvent.graphic.geometry || {};

                var lngFieldName = mapView.getLngFieldName();
                var latFieldName = mapView.getLatFieldName();
                var location = {};
                location[lngFieldName] = geometry.x;
                location[latFieldName] = geometry.y;
                return location;
            }
        })
    ;
    return ExGraphicsLayer;
});